<?php  if( !defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('has_add')) 
{
	/**
	 * has_add
	 * 
	 * Checks if user has add permissions for current controller
	 * 
	 * @access public
	 * @return boolean
	 */
	function has_add($url = NULL) 
	{
		$CI =& get_instance();
		$CI->load->library('Session');
		
		if ($CI->session->userdata('super') == 'Y') return TRUE;
		
		if (is_null($url)) $url = $CI->uri->segment(1);
		
		$CI->load->model('Auth_model');
		$where = array(
					'u.user_id' => $CI->session->userdata('user_id'),
					'url' => $url,
					'ap.a' => 1,
				);
				
		return $CI->Auth_model->has_permissions($where);
	}
}

if ( ! function_exists('has_edit')) 
{
	/**
	 * has_edit
	 * 
	 * Checks if user has edit permissions for current controller
	 * 
	 * @access public
	 * @return boolean
	 */
	function has_edit($url = NULL) 
	{
		$CI =& get_instance();
		$CI->load->library('session');
		
		if ($CI->session->userdata('super') == 'Y') return TRUE;
		
		if (is_null($url)) $url = $CI->uri->segment(1);
		
		$CI->load->model('Auth_model');
		$where = array(
					'u.user_id' => $CI->session->userdata('user_id'),
					'url' => $url,
					'ap.e' => 1,
				);
				
		return $CI->Auth_model->has_permissions($where);
	}
}

if ( ! function_exists('has_delete')) 
{
	/**
	 * has_delete
	 * 
	 * Checks if user has delete permissions for current controller
	 *
	 * @access public
	 * @return boolean
	 */
	function has_delete($url = NULL) 
	{
		$CI =& get_instance();
		$CI->load->library('session');
				
		if ($CI->session->userdata('super') == 'Y') return TRUE;
		
		if (is_null($url)) $url = $CI->uri->segment(1);
		
		$CI->load->model('Auth_model');
		$where = array(
					'u.user_id' => $CI->session->userdata('user_id'),
					'url' => $url,
					'ap.d' => 1,
				);
				
		return $CI->Auth_model->has_permissions($where);
	}
}

if ( ! function_exists('has_read')) 
{
	/**
	 * has_read
	 * 
	 * Checks if user has read permissions for current controller
	 *
	 * @access public
	 * @return boolean
	 */
	function has_read($url = NULL) 
	{
		$CI =& get_instance();
		$CI->load->library('session');
		
		if ($CI->session->userdata('super') == 'Y') return TRUE;
		
		if (is_null($url)) $url = $CI->uri->segment(1);
		
		$CI->load->model('Auth_model');
		$where = array(
					'u.user_id' => $CI->session->userdata('user_id'),
					'url' => $url,
					'ap.r' => 1,
				);
				
		return $CI->Auth_model->has_permissions($where);
	}
}

if ( ! function_exists('get_navigation')) 
{
	/**
	 * get_navigation
	 * 
	 * Gets navigation html code
	 * 
	 * @access public
	 * @return string
	 */
	function get_navigation() 
	{
		$CI =& get_instance();
		$CI->load->library('navigation');	
		
		$menu = $CI->navigation->create_navigation(); 
		$nav = '';
		foreach ($menu as $i => $value) 
		{
			$nav .= '<li>'. $value .'</li>';
		}
		
		return $nav;
	}
}
?>
